/* cgi.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cgi.h"

char* getcgidata(FILE* fp, char* requestmethod) {
    char* input;
    int len;
    int size = 1024;
    int i = 0;

    if (requestmethod==NULL) {
        input = getenv("QUERY_STRING");
        return input;
    } else if (!strcmp(requestmethod, "GET")) {
        input = getenv("QUERY_STRING");
        return input;
    } else if (!strcmp(requestmethod, "POST")) {
        len = atoi(getenv("CONTENT_LENGTH"));
        input = (char*) malloc(sizeof(char) * (size + 1));

        if (len == 0) {
            input[0] = '\0';
            return input;
        }

        while (1) {
            input[i] = (char) fgetc(fp);
            if (i == size) {
                input[i + 1] = '\0';
                return input;
            }

            --len;
            if (feof(fp) || (!(len))) {
                i++;
                input[i] = '\0';
                return input;
            }
            i++;

        }
    }
    return NULL;
}


PARAM getParam(char *input, char *paraName){

    PARAM param[50], res = {{0},{"null"}};

    int pi = 0, i = 0, j = 0;
    //
    char *p = param[pi].key;

        if (input==NULL){
          return res;
        }
    for (i = 0; i < (int) strlen(input); i++) {
        if (input[i] == '=') {
            p[j] = '\0';

            j=0;
            p = param[pi].value;
        } else if (input[i] == '&') {
            if(i>0) {
                if(input[i-1] == '&')
                    continue;
            }
            p[j] = '\0';
            j=0;
            pi++;

            if(pi< sizeof(param)/sizeof(PARAM)){
                p = param[pi].key;
            } else {
                break;
            }

        } else {
            p[j++] = input[i];
        }

    }

//  for(i=0; i<10; i++){
//      printf("p-  %s : %s<br> \n", param[i].key, param[i].value);
//  }

    for(i=0; i<sizeof(param)/sizeof(PARAM); i++){
        //printf("p-  %s : %s<br> \n", param[i].key, param[i].value);
        if( strcmp(param[i].key, paraName) ==0 ){
            return param[i];
        }
    }

    return res;
}

int getParams(char *input, char *paraName, PARAM *params){

    PARAM param[50], res = {{0},{"null"}};

    int pi = 0, i = 0, j = 0;
    //
    char *p = param[pi].key;

    if (input==NULL){
      return 0;
    }
    for (i = 0; i < (int) strlen(input); i++) {
        if (input[i] == '=') {
            p[j] = '\0';

            j=0;
            p = param[pi].value;
        } else if (input[i] == '&') {
            if(i>0) {
                if(input[i-1] == '&')
                    continue;
            }
            p[j] = '\0';
            j=0;
            pi++;

            if(pi< sizeof(param)/sizeof(PARAM)){
                p = param[pi].key;
            } else {
                break;
            }

        } else {
            p[j++] = input[i];
        }

    }

//  for(i=0; i<10; i++){
//      printf("p-  %s : %s<br> \n", param[i].key, param[i].value);
//  }
	//PARAM params[50];
	j = 0;
    for(i=0; i<sizeof(param)/sizeof(PARAM); i++){
        //printf("p-  %s : %s<br> \n", param[i].key, param[i].value);
        if( strcmp(param[i].key, paraName) ==0 ){
            //return param[i];
            params[j] = param[i];
            j++;
        }
    }

    return j;
}

